﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace ResxCrunch.Utils
{
    /// <summary>
    /// Template class for using as a base for injecting the singleton pattern
    /// </summary>
    /// <typeparam name="T">Type of the object that needs to be created</typeparam>
    public abstract class Singleton<T> where T : new()
    {
        /// <summary>
        /// Instance of the class. Lazy loaded because of the readonly modifier
        /// </summary>
        private static readonly T instance = new T();

        /// <summary>
        /// Initializes a new instance of the <see cref="Singleton&lt;T&gt;"/> class.
        /// </summary>
        protected Singleton()
        {
        }

        /// <summary>
        /// Gets the reference to the object here.
        /// </summary>
        public static T Instance
        {
            get
            {
                return Nested.instance;
            }
        }

        /// <summary>
        /// Needed for fully lazy thread safe implementation
        /// </summary>
        class Nested
        {
            /// <summary>
            /// Explicit static constructor to tell C# compiler not to mark type as beforefieldinit
            /// </summary>
            static Nested()
            {
            }

            internal static T instance = new T();
        }
    }
}
